$(function () {
    BASE.OTHER_BUYBOOK.getBookDetail();
});
var BASE = BASE||{};
BASE.OTHER_BUYBOOK = (function () {
    var EXPREETPL = null;
    var BANNER = null;
    /*增减购买的数量*/
    function addReduceNum(num) {
        var $buyNumber = $("#buyNumber");
        var oldbuyNum = Math.ceil($buyNumber.text());
        var price = $buyNumber.data('price');
        var currentBuynum = oldbuyNum+num;
        var expressPrice = 0;
        var total = 0;
        if(currentBuynum<=0){
            return;
        }
        $buyNumber.text(currentBuynum);
        if(EXPREETPL && num!=0){
            expressPrice = _calcPrice(EXPREETPL);
        }
        total = (currentBuynum * price+ expressPrice).toFixed(2);
        $('#buyTotal').text(total);
        $('#expressPrice').text('(含百世汇通快递费：'+expressPrice+'元)');
    }
    /*确认买书*/
    function buyBook(id,obj) {
        var sendData = {
            bookId:BASE.COMMON.getId().split('&num=')[0],
            num:Math.ceil($("#buyNumber").text()),
            addressId:id,
            expressCode:'huitong', //快递公司代码
            memberRemark:'',//客户备注
        };
        BASE.postHttp('/device/order/book/buy',sendData,function (res) {
            if(res.code!=200){
                BASE.clearpop(res.message);
                $(obj).attr('disabled',false);
                return;
            }
            location.href = '/userSubmitOrder.html?id='+res.data.id;
        })
    }

    /*获取运费*/
    function getExpressTpl(obj) {
        var provinceId = $(obj).data('provinceid')||$(obj).find('option:selected').val();
        if(!$(obj).is(':checked') && $(obj).hasClass('checkbox')){
            EXPREETPL = null;
            addReduceNum(0);
            return;
        }
        _setSelectone(obj);
        var sendData = {
            provinceId:provinceId,
            expressCode:'huitong',
        };
        BASE.apiGet('/device/expressTemplate/list',sendData,function (res) {
            var $buyNumber = $("#buyNumber");
            var expressPrice =  _calcPrice(res[0]);
            var num = Math.ceil($buyNumber.text());
            var price = $buyNumber.data('price');
            EXPREETPL = res[0];
            var total = num * price;
            var realTotal =(total+ expressPrice).toFixed(2);
            $('#expressPrice').text('(含百世汇通快递费：'+expressPrice+'元)');
            $('#buyTotal').text(realTotal);
        })
    }
    /*设置只选择一个*/
    function _setSelectone(obj) {
        if ($(obj).is(':checked')) {
            $("#addressList :checkbox").prop("checked", false);
            $(obj).prop("checked", true);
        }
    }
    /*运费计算*/
    function _calcPrice(tpl) {
        var $buyNumber = $("#buyNumber");
        var amount = $buyNumber.text()/1;
        var weight = amount * $buyNumber.data('weight');
        if (weight <= tpl.inRangeWeight) {
            return tpl.inRangePrice + tpl.packagePrice ;
        } else {
            //计算公式  总价 = 重量内的钱 + ((当前重量  - 范围类重量) * 超出范围每KG/元) + 包装费
            var w1 = weight - tpl.inRangeWeight;
            var expressPrice = tpl.outRangePrice*w1;
            expressPrice = expressPrice + tpl.inRangePrice+ tpl.packagePrice;
            return expressPrice;
        }
    }
    /*书籍提交够买*/
    function sureBuyPre(obj) {
        if(!BASE.COMMON.getUserId()){
            BASE.clearpop('请登录');
            location.href = '/login.html';
            return;
        }
        $(obj).attr('disabled',true);
        var num = $('#buyNumber').text().trim();
        location.href = '/'+BASE.COMMON.getId()+'/sureBuyBook.html?num='+num;
    }
    /*获取书籍详情*/
    function getBookDetail() {
        var num = location.href.split('num=').pop().split('&')[0];
        var id = BASE.COMMON.getId();
        BASE.apiGet('/device/book/detail',{id:id},function (res) {
            res.num =  num;
            res.total = num * res.price;
            $('#bookDetail').html(template('bookDetail_tpl',{data:res}));
        })
    }
    return {
        BANNER:BANNER,
        addReduceNum:addReduceNum,
        buyBook:buyBook,
        getExpressTpl:getExpressTpl,
        sureBuyPre:sureBuyPre,
        getBookDetail:getBookDetail,
    }
})();
